#!/usr/bin/env python
#-*- coding:utf-8 -*-
from flask import render_template, request, flash, redirect, url_for, make_response, abort
from werkzeug.utils import secure_filename

def init_views(app):



    @app.route('/user/<regex("[a-z]{3}"):user_id>')
    def user(user_id):
        return 'User %s' %user_id


    @app.route('/user/<username>')
    def hello(username):
        return username




    @app.route('/about')
    def about():
        return 'about'


    @app.route('/upload', methods=['GET', 'POST'])
    def file_upload():
        if request.method == 'POST':
            #print request.files
            f = request.files['file']
            #print f.filename
            basepath = path.abspath(path.dirname(__file__))
            print basepath
            upload_path = path.join(basepath,'static/uploads', secure_filename(f.filename))
            f.save(upload_path)
            return redirect(url_for('file_upload'))
        return render_template('upload.html')





    @app.template_test('current_link')
    def is_current_link(link):
        return link['href'] == request.path





    @app.template_filter('md')
    def mark_to_html(txt):
        from markdown import markdown
        return markdown(txt)


    def read_md(filename):
        with open(filename) as md_file:
            content = reduce(lambda x,y:x+y,md_file.readlines())
        return content.decode('utf-8')


    @app.context_processor
    def inject_methods():
        return dict(read_md = read_md)